package com.br.android.widingwheels;

import java.text.DecimalFormat;
import java.util.Currency;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.br.android.widingwheels.db.WidingWheelsDAO;
import com.br.android.widingwheels.entidades.Gasto;

public class ExibicaoGastoActivity extends SherlockActivity {

	private TextView textViewValor;
	private TextView textViewNomeGasto;
	private TextView textViewCategoria;
	private TextView textViewData;
	
	private Gasto gasto;
	
	Currency currency = Currency.getInstance(Locale.getDefault());
	private static DecimalFormat format;
	
	private WidingWheelsDAO dao;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_exibicao_gasto);
		
		textViewValor = (TextView) findViewById(R.id.textViewExibicaoValorGasto);
		textViewNomeGasto = (TextView) findViewById(R.id.textViewExibicaoNomeGasto);
		textViewCategoria = (TextView) findViewById(R.id.textViewExibicaoCategoria);
		textViewData = (TextView) findViewById(R.id.textViewExibicaoData);
		
		format = new DecimalFormat(currency.getSymbol() + " ###,###,##0.00");
		
		dao = new WidingWheelsDAO(this);
		gasto = new Gasto();
		Intent it = getIntent();
		gasto.setIdGasto(it.getExtras().getInt("idgasto"));
		gasto.setNomeGasto(it.getExtras().getString("descricao"));
		gasto.setValorGasto(it.getExtras().getDouble("valor"));
		gasto.setDataGasto(it.getExtras().getString("data"));
		gasto.setCategoriaGasto(it.getExtras().getString("categoria"));
		
		textViewValor.setText(format.format(gasto.getValorGasto()));
		textViewNomeGasto.setText(gasto.getNomeGasto());
		textViewCategoria.setText(gasto.getCategoriaGasto());
		textViewData.setText(gasto.getDataGasto());
		
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menu_exibicao_gasto, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuExcluirGasto:
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setTitle("Widing Wheels");
			dialog.setMessage("Tem certeza que deseja excluir este gasto?");
			dialog.setPositiveButton("Excluir", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dao.excluirGasto(gasto.getIdGasto());
					startActivity(new Intent(ExibicaoGastoActivity.this, PrincipalActivity.class));
					overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
					finish();
					mensagem("Gasto excluído.");
				}
			});
			dialog.setNegativeButton("Cancelar", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			dialog.show();
			break;
		
		case R.id.menuEditarGasto:
			Intent it = new Intent(this, EditarGastoActivity.class);
			it.putExtra("idgasto", gasto.getIdGasto());
			it.putExtra("descricao", gasto.getNomeGasto());
			it.putExtra("valor", gasto.getValorGasto());
			startActivity(it);
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
			break;
		
		case android.R.id.home:
			startActivity(new Intent(ExibicaoGastoActivity.this, PrincipalActivity.class));
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void mensagem(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	
}
